 do nolap
 else
; can be further reduced if we change cmndlist instead
wlapparm equ cmndlist+2 ; 3 bytes as parameter to call the card
writelap equ *
 sta wlapparm+1
 iny
 lda (cmndlist),y
 sta wlapparm+2
 iny ; now y = 3
 sty wlapparm
 ldy #0
 ldx #>wlapparm
 jsr callapbus ; call the card
 sta status
 rts
 fin
;
wdsadr equ cmndlist+2
sizeinzp equ cmndlist+4
databuf equ cmndlist+6
;
writeddp equ *
 php
 sei ; disable interrupt
 sta ddphead+ld.protocol
 iny
 lda (cmndlist),y
 beq wddp.8a ; socket 0 is illegal
 jsr srchscktable ; is socket open ?
 beq wddp.8b ; yes, proceed
wddp.8a lda #scktnotopen ; else error
 bne wddp.9 ; bra
wddp.8b jsr setdestadr
 lda (cmndlist),y ; wds address
 tax
 iny
 lda (cmndlist),y
 tay
 jsr dowrtddp
wddp.9 plp
wddp.9a sta status
 rts
;
setdestadr equ * ; before input set y to 2 and acc to srce socket
 sta ddphead+sd.ssckt
 iny
 lda (cmndlist),y ; network high byte
 sta ddphead+ld.dnet
 iny
 lda (cmndlist),y
 sta ddphead+ld.dnet+1
 iny
 lda (cmndlist),y ; destination node
 sta lapdest
 iny
 lda (cmndlist),y ; get destination socket
 sta ddphead+sd.dsckt
 iny
 lda (cmndlist),y
 and #$01
 sta chksmf ; checksum flag
 iny
 rts
;
dowrtddp equ * ; make a ddp packet and write it out
 stx wdsadr ; wds address
 stx wddptbl
 sty wdsadr+1
 sty wddptbl+1
 lda ddphead+ld.dnet ; get network
 ldx ddphead+ld.dnet+1
 bne chknet
 tay ; if low byte is 0, how about high byte
 beq wrtshort ; network = 0, do short ddp
chknet cpx thisnet+1 ; check to see if destination net is local
 bne wrtlong
 cmp thisnet  
 bne wrtlong
 do savespace
wrtshort ldy #move3-movetable
 jsr movedata
 beq wddp.2 ; since movedata set z flag, bra
wrtlong ldy #move2-movetable
 jsr movedata
 jsr headersum ; calcuate checksum of the header
 ldx a.bridge ; do we have a bridge
 bne wddp.1a ; yes, then send packet to bridge
 lda thisnet ; no bridge, then try it on same network
 ora thisnet+1 ; do we have a network number ?
 beq wddp.2 ; no, then send to same node else force error
wddp.1a stx lapdest ; send to bridge or node 0 (force error)
 else
wrtshort lda #1
 sta laptype
 lda ddphead+ld.protocol
 sta ddphead+sd.protocol
 lda #5
 sta ddphead+sd.length+1
 lda #8
 sta wddpsize
 lda #0
 sta ddphead+sd.length
 sta chksmf ; never do checksum on short ddp
 beq wddp.2 ; bra
wrtlong lda #2
 sta laptype ; long ddp is protocol 2
 lda ddphead+sd.ssckt
 sta ddphead+ld.ssckt
 lda ddphead+sd.dsckt
 sta ddphead+ld.dsckt
 lda #16 ; lap + long atp header is 16 bytes
 sta wddpsize
 lda #13
 sta ddphead+ld.length+1
 lda lapdest
 sta ddphead+ld.dnode
 lda ournode
 sta ddphead+ld.snode
 lda thisnet
 sta ddphead+ld.snet
 lda thisnet+1
 sta ddphead+ld.snet+1
 jsr headersum ; calcuate checksum of the header
 sta ddphead+ld.length ; headersum return acc = 0
 sta ddphead+ld.chksum ; for the case of no checksum
 sta ddphead+ld.chksum+1
 ldx a.bridge ; do we have a bridge
 bne wddp.1a ; yes, then send packet to bridge
 lda thisnet ; no bridge, then try it on same network
 ora thisnet+1 ; do we have a network number ?
 beq wddp.2 ; no, then send to same node
 lda #toomanycollison ; else error, no bridge connected
 jmp wddp.9a ; error return
wddp.1a stx lapdest ; send to bridge or node 0 (force error)
 fin
wddp.2 ldy #3 ; copy our header addr to first 4 byte of wds
wddp.3 lda wddpsize,y
 sta (wdsadr),y
 dey
 bpl wddp.3
wddp.4 clc  ; first adjust for size of datagram header
 lda #4
 adc wdsadr
 sta wdsadr
 bcc *+4
 inc wdsadr+1
 ldy #3
wddp.5 lda (wdsadr),y
 sta sizeinzp,y
 dey
 bpl wddp.5
 cpy sizeinzp+1 ; since y is $ff, check for end of list
 beq wddp.6
 clc ; note accumator is sizeinzp
 adc ddphead+sd.length+1
 sta ddphead+sd.length+1
 lda sizeinzp+1
 adc ddphead+sd.length
 sta ddphead+sd.length
 jsr dochecksum
 beq wddp.4 ; since dochecksum always set z flag, bra
;
wddp.6 lda chksmf
 beq wddp.7
 lda curcksum
 ldx curcksum+1
 bne *+7 ; if low byt not 0, checksum non-zero
 tay ; is high byte 0
 bne *+4 ; no, then checksum not zero
 dex ; x = $ff
 txa ; a = $ff
 sta ddphead+ld.chksum
 stx ddphead+ld.chksum+1
wddp.7 equ *
 ifne opers-pascal
 ldy #<wddpparm
 ldx #>wddpparm
 else
 ldy adrwddpparm+1
 ldx adrwddpparm
 fin
;
; jsr callapbus       ; replace by fall through
; rts
;
callapbus jsr applebus
 beq callap.rts
 adc #$5f ; if error, carry was set
callap.rts rts
;
;
;
headersum ldx #4 ; set up parameter to checksum the header
hdrsum.1 lda chksumhead-1,x
 sta sizeinzp-1,x
 dex
 bne hdrsum.1
 stx curcksum ; initial current checksum to 0
 stx curcksum+1
;
; fall through to dochecksum
;
dochecksum lda chksmf ; do checksum ?
 bne mustchksum ; definitely do checksum
 rts ; else return with z flag set
;
;
rdatpheader equ * ; read the atp header and calculate checksum
 ldy #<ratpparm
 ldx #>ratpparm
;
; caution, readhdrsum destorys many zero page locations
;
readhdrsum equ * ; read the header and calculate the checksum
 sty databuf+1 ; save the parmlist pointer we are using
 stx databuf
 jsr readheader
 bne chksum.9
 lda chksmf
 beq chksum.9
 ldy #4
 lda (databuf),y
 sta sizeinzp+1
 dey
 lda (databuf),y
 sta sizeinzp
 dey
 lda (databuf),y
 tax
 dey
 lda (databuf),y
 sta databuf
 stx databuf+1
;
mustchksum ldx sizeinzp ; put low byte of byte count in x
chksum.0 ldy #0
chksum.1 txa ; look at low byte of byte count
 bne chksum.2 ; then we have not reach 0, just dec low byte
 dec sizeinzp+1 ; else also decrement high byte
 bmi chksum.8 ; if high byte was 0, done
chksum.2 dex
 clc
 lda (databuf),y
 adc curcksum+1
 sta curcksum+1
 bcc chksum.3
 inc curcksum
chksum.3 rol a ; high bit of low byte into carry
 rol curcksum ; high bit of low byte into low bit of high byte
 rol curcksum+1 ; high bit of high byte into low bit of low byte
 iny
 bne chksum.1
 inc databuf+1
 jmp chksum.0
chksum.8 txa ; set z flag
chksum.9 rts
;
chksumhead dw 9,ddphead+4
;
 ifeq opers-pascal
adrwddpparm dw wddpparm
 fin
